package com.infore.airsupervise.ui.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.AwesomeTextView;
import com.beardedhen.androidbootstrap.font.FontAwesome;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infore.airsupervise.R;
import com.infore.airsupervise.adapter.CacheFragAdapter;
import com.infore.airsupervise.ui.fragment.DispatchAQIFragment;
import com.infore.airsupervise.ui.fragment.DispatchSynthesisFragment;
import com.infore.airsupervise.ui.fragment.DispatchWeatherFragment;
import com.infore.airsupervise.ui.widget.ItemDecoration1px;
import com.infore.airsupervise.ui.widget.NoScrollViewPager;
import com.infore.airsupervise.utils.DialogUtil;
import com.rey.material.app.DialogFragment;
import com.rey.material.app.SimpleDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 实时调度
 * <p>
 * 作者: dingxinlin
 * 创建时间: 2017/8/16 17:23
 */
public class DispatchActivity extends BaseActivity {

    @BindView(R.id.titlebar_title)
    TextView titlebarTitle;
    @BindView(R.id.titlebar_right_menu)
    AwesomeTextView titlebarRightMenu;
    @BindView(R.id.tab_dispatch)
    TabLayout tabDispatch;
    @BindView(R.id.view_pager_dispatch)
    NoScrollViewPager viewPager;

    private int tabIndex = 0; // 记录tab索引
    private int dataType = 0; // 记录数据类型
    private String[] dataTypeArr = new String[]{"分钟数据", "小时数据", "日数据"};

    private OnDataTypeChangeListener aqiListener, weatherListener, synthesisListener; // 数据类型切换监听者

    /**
     * 数据类型切换监听接口类
     */
    public interface OnDataTypeChangeListener {
        /**
         * 数据类型切换
         * @param dataType 0：分钟数据，1：小时数据，2：日数据
         */
        void onDataTypeChange(int dataType);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dispatch);
        ButterKnife.bind(this);

        initView();
    }

    private void initView() {
        // 设置标题栏
        setActivityTitle();
        titlebarRightMenu.setFontAwesomeIcon(FontAwesome.FA_FILTER);

        //设置TabLayout
        tabDispatch.addTab(tabDispatch.newTab().setText("AQI数据"));
        tabDispatch.addTab(tabDispatch.newTab().setText("气象数据"));
        tabDispatch.addTab(tabDispatch.newTab().setText("综合指数"));

        // TabLayout选择监听
        tabDispatch.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tabIndex = tab.getPosition();
                dataType = 0;
                setActivityTitle();

                // 切换Fragment显示
                viewPager.setCurrentItem(tabIndex);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {}
            @Override
            public void onTabReselected(TabLayout.Tab tab) {}
        });

        // 实例Fragment
        DispatchAQIFragment aqiFragment = new DispatchAQIFragment();
        DispatchWeatherFragment weatherFragment = new DispatchWeatherFragment();
        DispatchSynthesisFragment synthesisFragment = new DispatchSynthesisFragment();
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(aqiFragment);
        fragments.add(weatherFragment);
        fragments.add(synthesisFragment);

        // 设置监听
        aqiListener = aqiFragment;
        weatherListener = weatherFragment;
        synthesisListener = synthesisFragment;

        // 设置适配器
        CacheFragAdapter fragAdapter = new CacheFragAdapter(getSupportFragmentManager(), fragments, null);
        viewPager.setAdapter(fragAdapter);
    }

    /**
     * 条件筛选点击
     */
    @OnClick(R.id.titlebar_right_menu)
    void onRightBtnClick() {
        DialogUtil.getInstance().radioChoice("条件筛选", dataTypeArr, dataType,
                new DialogUtil.OnDialogActionListener(){

                    @Override
                    public void onPositiveActionClicked(DialogFragment fragment) {
                        SimpleDialog simpleDialog = (SimpleDialog) fragment.getDialog();

                        dataType = simpleDialog.getSelectedIndex();
                        setActivityTitle();

                        // 通知Fragment刷新
                        if (tabIndex == 0) {
                            aqiListener.onDataTypeChange(dataType);
                        } else if (tabIndex == 1) {
                            weatherListener.onDataTypeChange(dataType);
                        } else {
                            synthesisListener.onDataTypeChange(dataType);
                        }
                    }

                    @Override
                    public void onNegativeActionClicked(DialogFragment fragment) {}
                })
                .show(getSupportFragmentManager(), "tag");
    }

    /**
     * 设置Activity标题
     */
    private void setActivityTitle() {
        String[] tabStrs = new String[]{"AQI", "气象", "综合指数"};

        StringBuffer stringBuffer = new StringBuffer();

        for (int j=0; j<tabStrs.length; j++) {
            if (j == tabIndex) {
                stringBuffer.append(tabStrs[j]);
                break;
            }
        }

        for (int i=0; i<dataTypeArr.length; i++) {
            if (i == dataType) {
                stringBuffer.append(dataTypeArr[i]);
                break;
            }
        }

        titlebarTitle.setText(stringBuffer.toString());
    }

    /**
     * 返回按钮点击
     */
    @OnClick(R.id.titlebar_left_menu)
    void onLeftBtnClick() {
        finish();
    }

}
